Repository navigation
feat(postgrest): add automatic retries for transient errors - #2072
Conversation
|
Important Review skippedDraft detected. Please check the settings in the CodeRabbit UI or the You can disable this status message by setting the
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Fix all issues with AI agents
In `@packages/core/postgrest-js/src/PostgrestBuilder.ts`:
- Around line 193-244: The retry loop in executeWithRetry currently retries on
520 responses without consuming the response body and also may retry on
AbortError; update executeWithRetry so that when shouldRetry(...) returns true
you first fully drain the response body (e.g., await res.arrayBuffer() or
res.text() to consume streams) before sleeping/retrying, and in the catch block
immediately rethrow if fetchError.name === 'AbortError' (do not treat as
retryable), otherwise keep existing logic using RETRYABLE_METHODS,
DEFAULT_MAX_RETRIES, getRetryDelay, sleep, and continue calling _fetch and
processResponse as before.
🧹 Nitpick comments (3)
packages/core/postgrest-js/test/retry.test.ts (1)
210-235: Consider a more robust approach for verifying backoff delays.The current implementation spies on
setTimeoutand immediately executes callbacks withdelay=0. While functional, this approach bypasses the actual timing behavior and could mask issues if the retry implementation changes how it schedules delays.A more robust alternative would use
vi.getTimerCount()or advance timers by specific amounts and verify fetch call counts at each interval.packages/core/postgrest-js/src/PostgrestQueryBuilder.ts (2)
24-25: Add JSDoc documentation for theretryproperty.The
retryproperty is part of the public API but lacks JSDoc documentation. Other public properties in this class could benefit from similar documentation, but at minimum the newretryproperty should be documented to explain its purpose and default behavior.📝 Suggested documentation
+ /** + * Enable or disable automatic retries for transient errors. + * When enabled, idempotent requests (GET/HEAD/OPTIONS) that fail with 520 errors + * will be automatically retried with exponential backoff. + */ // Retry configuration retry?: booleanAs per coding guidelines: "Use JSDoc comments for all public APIs in each library".
40-59: Addretryparameter to the constructor's JSDoc.The constructor example and parameter documentation should include the new
retryoption for completeness.📝 Suggested documentation update
/** * Creates a query builder scoped to a Postgres table or view. * * `@example` * ```ts * import PostgrestQueryBuilder from '@supabase/postgrest-js' * * const query = new PostgrestQueryBuilder( * new URL('https://xyzcompany.supabase.co/rest/v1/users'), - * { headers: { apikey: 'public-anon-key' } } + * { headers: { apikey: 'public-anon-key' }, retry: true } * ) * ``` + * + * `@param` url - The URL for the query + * `@param` options - Named parameters + * `@param` options.headers - Custom headers + * `@param` options.schema - Postgres schema + * `@param` options.fetch - Custom fetch function + * `@param` options.retry - Enable automatic retries for transient errors */
3734cba to
8c9359d
Compare
@supabase/auth-js
@supabase/functions-js
@supabase/postgrest-js
@supabase/realtime-js
@supabase/storage-js
@supabase/supabase-js
commit: |
274617c to
7f6bc9b
Compare
The lack of retry header is concerning, without a retry header it will be too hard to debug at the origin server. Can that be added in this PR? |
|
@steve-chavez I was worried about breaking changes. Since we now have https://supabase.com/docs/guides/functions/cors we can rely on this and hope users will be using it. I will add back the headers. Any thoughts about the 5xx errors? |
30c0fc8 to
1096c54
Compare
steve-chavez
left a comment
There was a problem hiding this comment.
503 and 520 LGTM to retry on.
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 18 out of 18 changed files in this pull request and generated 8 comments.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Implement automatic retry logic for PostgREST SDK requests that fail with 520 status codes (Cloudflare timeout/connection errors). Key features: - Enabled by default for GET/HEAD/OPTIONS requests - 3 retry attempts with exponential backoff (1s, 2s, 4s) - X-Retry-Count header sent for observability - Can be disabled globally or per-request via .retry(false) This addresses customer feedback about perceived reliability issues when transient 520 errors occur under load. Related: https://linear.app/supabase/project/automatic-retries-for-postgrest-sdk-c6fa4eaf163a Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This PR updates `@supabase/supabase-js` to v2.102.1. **Source**: supabase-js-stable-release --- ## Release Notes ## v2.102.1 ## 2.102.1 (2026-04-07) ### 🩹 Fixes - **functions:** add toJSON to FunctionsError for correct JSON serialization ([#2226](supabase/supabase-js#2226)) ### ❤️ Thank You - oniani1 ## v2.102.0 ## 2.102.0 (2026-04-07) ### 🚀 Features - **postgrest:** add automatic retries for transient errors ([#2072](supabase/supabase-js#2072)) - **postgrest:** add success discriminator field to PostgREST response types ([#2198](supabase/supabase-js#2198)) - **supabase:** export PostgrestFilterBuilder and StorageApiError from supabase-js ([#2222](supabase/supabase-js#2222)) ### 🩹 Fixes - **auth:** downgrade console.error to console.warn for missing session ([#2214](supabase/supabase-js#2214)) - **ci:** add --ignore-scripts to platform test installs to block post install attacks ([#2206](supabase/supabase-js#2206)) - **postgrest:** add toJSON to PostgrestError for correct JSON serialization ([#2212](supabase/supabase-js#2212)) - **postgrest:** reject excess properties in insert, update, and upsert ([#2186](supabase/supabase-js#2186)) - **storage:** set correct content-type for uploads ([#2211](supabase/supabase-js#2211)) - **storage:** avoid duplicate content-type headers in vector requests ([#2220](supabase/supabase-js#2220)) ### ❤️ Thank You - Clay - Guilherme Souza - Katerina Skroumpelou @mandarini - oniani1 - Vaibhav @7ttp This PR was created automatically. Co-authored-by: supabase-workflow-trigger[bot] <266661614+supabase-workflow-trigger[bot]@users.noreply.github.com>
This PR updates @supabase/*-js libraries to version 2.102.1. **Source**: supabase-js-stable-release **Changes**: - Updated @supabase/supabase-js to 2.102.1 - Updated @supabase/auth-js to 2.102.1 - Updated @supabase/realtime-js to 2.102.1 - Updated @supabase/postgest-js to 2.102.1 - Refreshed pnpm-lock.yaml --- ## Release Notes ## v2.102.1 ## 2.102.1 (2026-04-07) ### 🩹 Fixes - **functions:** add toJSON to FunctionsError for correct JSON serialization ([#2226](supabase/supabase-js#2226)) ### ❤️ Thank You - oniani1 ## v2.102.0 ## 2.102.0 (2026-04-07) ### 🚀 Features - **postgrest:** add automatic retries for transient errors ([#2072](supabase/supabase-js#2072)) - **postgrest:** add success discriminator field to PostgREST response types ([#2198](supabase/supabase-js#2198)) - **supabase:** export PostgrestFilterBuilder and StorageApiError from supabase-js ([#2222](supabase/supabase-js#2222)) ### 🩹 Fixes - **auth:** downgrade console.error to console.warn for missing session ([#2214](supabase/supabase-js#2214)) - **ci:** add --ignore-scripts to platform test installs to block post install attacks ([#2206](supabase/supabase-js#2206)) - **postgrest:** add toJSON to PostgrestError for correct JSON serialization ([#2212](supabase/supabase-js#2212)) - **postgrest:** reject excess properties in insert, update, and upsert ([#2186](supabase/supabase-js#2186)) - **storage:** set correct content-type for uploads ([#2211](supabase/supabase-js#2211)) - **storage:** avoid duplicate content-type headers in vector requests ([#2220](supabase/supabase-js#2220)) ### ❤️ Thank You - Clay - Guilherme Souza - Katerina Skroumpelou @mandarini - oniani1 - Vaibhav @7ttp ## v2.101.1 ## 2.101.1 (2026-03-31) ### 🩹 Fixes - **storage:** support exactOptionalPropertyTypes ([#2200](supabase/supabase-js#2200)) ### ❤️ Thank You - Vaibhav @7ttp This PR was created automatically. Co-authored-by: supabase-workflow-trigger[bot] <266661614+supabase-workflow-trigger[bot]@users.noreply.github.com>
Description
What changed?
Adds automatic retry logic to
@supabase/postgrest-jsfor two classes of transient errors:PGRST002schema cache loading on a replica) — PostgREST explicitly signals retryability; when aRetry-Afterheader is present, its value is used as the delayImplementation details:
AbortErroris rethrown immediately without retryingAbortSignal— aborting mid-wait resolves immediatelyX-Retry-Countrequest header is added on retry attempts (requested by PostgREST author for observability)New
retryoption onPostgrestClient:Key design decisions:
Retry-AfterheaderRetry-After: 0for PGRST002 — no unnecessary delayX-Retry-CountheaderWhy was this change needed?
Customers using load-balanced Supabase URLs and Cloudflare-fronted deployments experience intermittent failures that are safe to retry automatically. Without retries, these transient errors surface as real failures, making PostgREST appear unreliable even though the underlying request would succeed on a second attempt.
Screenshots/Examples
N/A
Breaking changes
Checklist
<type>(<scope>): <description>npx nx formatto ensure consistent code formatting